package com.cooper.base.service;

import com.cooper.base.model.SysMenu;
import com.cooper.base.model.SysRoleMenu;
import com.cooper.common.core.service.BaseService;
import com.cooper.common.exception.GeneralException;
import com.cooper.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色菜单Service(即角色权限配置)
 */
@Service("com.cooper.base.service.SysRoleMenuService")
public class SysRoleMenuService extends BaseService<SysRoleMenu> {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 根据角色Id获取该角色下所有Menu
     */
    public List<SysMenu> listByRole(String roleId) {
        if (StringUtil.isEmptyString(roleId)) {
            throw new GeneralException("角色Id为空");
        }
        Example roleMenuExample = new Example(SysRoleMenu.class);
        roleMenuExample.createCriteria().andEqualTo("roleId", roleId);
        return mapper.selectByExample(roleMenuExample).stream().map(roleMenu -> {
            return sysMenuService.selectByKey(roleMenu.getMenuId());
        }).collect(Collectors.toList());
    }
}
